package com.rdc.qingkou.model;

import android.util.Log;

import com.jakewharton.retrofit2.adapter.rxjava2.HttpException;
import com.rdc.qingkou.contract.SubscribeContract;
import com.rdc.qingkou.entity.MessageResult;
import com.rdc.qingkou.entity.SubscribeListResult;
import com.rdc.qingkou.entity.SubscribeUserResult;
import com.rdc.qingkou.model.imodel.OnHttpCallBack;
import com.rdc.qingkou.retrofitService.ISubscribeService;
import com.rdc.qingkou.utils.MyUtils;

import java.net.ConnectException;
import java.net.SocketTimeoutException;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class SubscribeModel implements SubscribeContract.ISubscribeModel{

    @Override
    public void getMessage(Integer integer, OnHttpCallBack<MessageResult> callBack) {

    }

    @Override
    public void subscribeUser(Integer userId, Integer subscribeId,final OnHttpCallBack<SubscribeUserResult> callBack) {

        MyUtils.getRetrofitProvider("")          //保存sessionId(必要)
                .create(ISubscribeService.class)
                .subscribeUser(userId,subscribeId)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.newThread())
                .subscribe(new Observer<SubscribeUserResult>() {

                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                    }

                    @Override
                    public void onNext(SubscribeUserResult result) {

                        callBack.onSuccessful(result);
                    }

                    @Override
                    public void onError(Throwable t) {
                        Log.d("MessageModel","onError");
                        t.printStackTrace();
                        if (t instanceof HttpException) {
                            HttpException httpException = (HttpException) t;
                            int code = httpException.code();
                            if (code == 500 || code == 404) {
                                callBack.onFaild("服务器出错");
                            }
                        } else if (t instanceof ConnectException) {
                            callBack.onFaild("网络断开,请打开网络!");
                        } else if (t instanceof SocketTimeoutException) {
                            callBack.onFaild("网络连接超时!!");
                        } else {
                            callBack.onFaild("发生未知错误" + t.getMessage());
                            //KLog.e(t.getMessage());
                        }
                    }
                    @Override
                    public void onComplete() {
                        Log.d("MessageModel","onComplete");
                    }
                });
    }

    @Override
    public void getSubscribeList(Integer userId ,final OnHttpCallBack<SubscribeListResult> callBack) {

        MyUtils.getRetrofitProvider("")
                .create(ISubscribeService.class)
                .getSubscribeList(userId)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.newThread())
                .subscribe(new Observer<SubscribeListResult>() {

                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Log.d("MessageModel","onSubscribe");
                    }

                    @Override
                    public void onNext(SubscribeListResult messageNewsNumResult) {


                        Log.d("MessageModel","onNext");
                        callBack.onSuccessful(messageNewsNumResult);
                    }

                    @Override
                    public void onError(Throwable t) {
                        Log.d("MessageModel","onError");
                        t.printStackTrace();
                        //失败的时候回调-----一下可以忽略 直接 callBack.onFaild("请求失败");
                        if (t instanceof HttpException) {
                            HttpException httpException = (HttpException) t;
                            //httpException.response().errorBody().string()
                            int code = httpException.code();
                            if (code == 500 || code == 404) {
                                callBack.onFaild("服务器出错");
                            }
                        } else if (t instanceof ConnectException) {
                            callBack.onFaild("网络断开,请打开网络!");
                        } else if (t instanceof SocketTimeoutException) {
                            callBack.onFaild("网络连接超时!!");
                        } else {
                            callBack.onFaild("发生未知错误" + t.getMessage());
                            //KLog.e(t.getMessage());
                        }
                    }

                    @Override
                    public void onComplete() {
                        Log.d("MessageModel","onComplete");
                    }
                });
    }

    @Override
    public void getFansList(Integer userId,final OnHttpCallBack<SubscribeListResult> callBack) {

        MyUtils.getRetrofitProvider("")
                .create(ISubscribeService.class)
                .getFansList(userId)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.newThread())
                .subscribe(new Observer<SubscribeListResult>() {

                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Log.d("MessageModel","onSubscribe");
                    }

                    @Override
                    public void onNext(SubscribeListResult messageNewsNumResult) {


                        Log.d("MessageModel","onNext");
                        callBack.onSuccessful(messageNewsNumResult);
                    }

                    @Override
                    public void onError(Throwable t) {
                        Log.d("MessageModel","onError");
                        t.printStackTrace();
                        //失败的时候回调-----一下可以忽略 直接 callBack.onFaild("请求失败");
                        if (t instanceof HttpException) {
                            HttpException httpException = (HttpException) t;
                            //httpException.response().errorBody().string()
                            int code = httpException.code();
                            if (code == 500 || code == 404) {
                                callBack.onFaild("服务器出错");
                            }
                        } else if (t instanceof ConnectException) {
                            callBack.onFaild("网络断开,请打开网络!");
                        } else if (t instanceof SocketTimeoutException) {
                            callBack.onFaild("网络连接超时!!");
                        } else {
                            callBack.onFaild("发生未知错误" + t.getMessage());
                            //KLog.e(t.getMessage());
                        }
                    }

                    @Override
                    public void onComplete() {
                        Log.d("MessageModel","onComplete");
                    }
                });

    }
}
